package cn.edu.zafu.bigdata.YinMingyan.chapter05;

/**
 * Represents a cube with length, width, and height.
 */
public class Cube {
    private double length;
    private double width;
    private double height;

    /**
     * Constructs a Cube with the specified dimensions.
     *
     * @param length the length of the cube
     * @param width the width of the cube
     * @param height the height of the cube
     */
    public Cube(double length, double width, double height) {
        this.length = length;
        this.width = width;
        this.height = height;
    }

    /**
     * Returns the length of the cube.
     *
     * @return the length of the cube
     */
    public double getLength() {
        return length;
    }

    /**
     * Returns the width of the cube.
     *
     * @return the width of the cube
     */
    public double getWidth() {
        return width;
    }

    /**
     * Returns the height of the cube.
     *
     * @return the height of the cube
     */
    public double getHeight() {
        return height;
    }

    /**
     * Indicates whether some other object is equal to this Cube.
     * Two cubes are equal if they have the same length, width, and height.
     *
     * @param obj the reference object with which to compare
     * @return true if this cube is the same as the obj argument; false otherwise
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        Cube other = (Cube) obj;
        return Double.compare(other.length, length) == 0 &&
                Double.compare(other.width, width) == 0 &&
                Double.compare(other.height, height) == 0;
    }

    /**
     * Returns a hash code value for the cube.
     *
     * @return a hash code value for the cube
     */
    //返回对象哈希码，对equals 方法的补充，确保如果两个对象相等
    @Override
    public int hashCode() {
        int result = 17;
        result = 31 * result + Double.hashCode(length);
        result = 31 * result + Double.hashCode(width);
        result = 31 * result + Double.hashCode(height);
        return result;
    }
}